package client;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MenuPanel extends JPanel {
	private MainViewJFrame mainViewJFrame;
	private Model model;
	
	private ArrayList<Observer> observers;

	private boolean changed;
	
	private JLabel nameLabel;
	private JLabel attributeLabel;
	private JButton checkButton;
	private JTextField betTextField;
	private JButton betButton;
	private JTextField raiseTextField;
	private JButton raiseButton;
	private JButton callButton;
	private JButton foldButton;
	private JButton allInButton;
	private JLabel yourBetLabel;
	private JLabel bankrollLabel;
	
	public String getText;
	
	public MenuPanel(final MainViewJFrame mainViewJFrame, final Model model) {
		super();
		this.mainViewJFrame = mainViewJFrame;
		this.model = model;

		setLayout(new GridLayout(12, 1, 5, 5));
		setBackground(new Color(210, 210, 210));

		nameLabel = new JLabel("Name");
		add(nameLabel);
		attributeLabel = new JLabel("");
		add(attributeLabel);

		checkButton = new JButton("Check");
		checkButton.setEnabled(false);
		add(checkButton);
		checkButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				model.setPressedButton("Check");
				mainViewJFrame.answearTextField.setText("You pressed \"Check\" button.");
				mainViewJFrame.setChanged();
				mainViewJFrame.notifyObservers();
			}
		});
		
		betTextField = new JTextField("Enter value...");
		betTextField.setEnabled(false);
		add(betTextField);
		
		betButton = new JButton("Bet");
		betButton.setEnabled(false);
		add(betButton);
		betButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					if ((Integer.parseInt(betTextField.getText()) < (model.getMaxBetNow()+1)) || (Integer.parseInt(betTextField.getText()) > model.getPlayerBankrollNow()))  {
						mainViewJFrame.answearTextField.setText("Value must be natural digit from " + (model.getMaxBetNow()+1) + " to " + model.getPlayerBankrollNow() + ".");
					} else {
						model.setPressedButton("Bet");
						if (model.getWhoHasSmallBlind().equals(model.getPlayerID())) {
							model.setBetValue(Integer.toString((Integer.parseInt(betTextField.getText())-(Integer.parseInt(model.getSmallBlind())))));
						} else if (model.getWhoHasBigBlind().equals(model.getPlayerID())) {
							model.setBetValue(Integer.toString((Integer.parseInt(betTextField.getText())-(Integer.parseInt(model.getBigBlind())))));
						} else {
							model.setBetValue(betTextField.getText());
						}
						mainViewJFrame.answearTextField.setText("You pressed \"Bet\" button.");
						mainViewJFrame.setChanged();
						mainViewJFrame.notifyObservers();
					}
				} catch (NumberFormatException ex) {
					mainViewJFrame.answearTextField.setText("Value must be natural digit from " + (model.getCallValue()+1) + " to " + model.getPlayerBankrollNow() + ".");
				}
			}
		});
		raiseTextField = new JTextField("Enter value...");
		raiseTextField.setEnabled(false);
		add(raiseTextField);
		
		raiseButton = new JButton("Raise");
		raiseButton.setEnabled(false);
		add(raiseButton);
		raiseButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					if ((Integer.parseInt(raiseTextField.getText()) < (model.getMaxBetNow()+1)) || (Integer.parseInt(raiseTextField.getText()) > model.getPlayerBankrollNow()))  {
						mainViewJFrame.answearTextField.setText("Value must be natural digit from " + (model.getMaxBetNow()+1) + " to " + model.getPlayerBankrollNow() + ".");
					} else {
						model.setPressedButton("Raise");
						if (model.getWhoHasSmallBlind().equals(model.getPlayerID())) {
							model.setRaiseValue(Integer.toString((Integer.parseInt(raiseTextField.getText())-(Integer.parseInt(model.getSmallBlind())))));
						} else if (model.getWhoHasBigBlind().equals(model.getPlayerID())) {
							model.setRaiseValue(Integer.toString((Integer.parseInt(raiseTextField.getText())-(Integer.parseInt(model.getBigBlind())))));
						} else {
							model.setRaiseValue(raiseTextField.getText());
						}
						mainViewJFrame.answearTextField.setText("You pressed \"Raise\" button.");
						mainViewJFrame.setChanged();
						mainViewJFrame.notifyObservers();
					}
				} catch (NumberFormatException ex) {
					mainViewJFrame.answearTextField.setText("Value must be natural digit from " + (model.getMaxBetNow()+1) + " to " + model.getPlayerBankrollNow() + ".");
				}
			}
		});
		callButton = new JButton("Call");
		callButton.setEnabled(false);
		add(callButton);
		callButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				model.setPressedButton("Call");
				mainViewJFrame.answearTextField.setText("You pressed \"Call\" button.");
				mainViewJFrame.setChanged();
				mainViewJFrame.notifyObservers();
			}
		});
		foldButton = new JButton("Fold");
		foldButton.setEnabled(false);
		add(foldButton);
		foldButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				model.setPressedButton("Fold");
				mainViewJFrame.answearTextField.setText("You pressed \"Fold\" button.");
				mainViewJFrame.setChanged();
				mainViewJFrame.notifyObservers();
			}
		});
		allInButton = new JButton("All-in");
		allInButton.setEnabled(false);
		add(allInButton);
		allInButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				model.setPressedButton("All-in");
				mainViewJFrame.answearTextField.setText("You pressed \"All-in\" button.");
				mainViewJFrame.setChanged();
				mainViewJFrame.notifyObservers();
			}
		});

		yourBetLabel = new JLabel("Your bet: 0T");
		add(yourBetLabel);

		bankrollLabel = new JLabel("BANKROLL:");
		add(bankrollLabel);

	}

	public void setNameLabelText(String setText) {
		this.nameLabel.setText(setText);
	}

	public void setAttributeLabelText(String setText) {
		this.attributeLabel.setText(setText);
	}

	public void setBankrollLabelText(String setText) {
		this.bankrollLabel.setText(setText);
	}
	
	public void setYourBetLabelText(String setText) {
		this.yourBetLabel.setText(setText);
	}


	public void setCheckButtonEnabled() {
		this.checkButton.setEnabled(true);
	}
	
	public void setCheckButtonDisabled() {
		this.checkButton.setEnabled(false);
	}

	public void setBetTextFieldEnabled() {
		this.betTextField.setEnabled(true);
	}
	
	public void setBetTextFieldDisabled() {
		this.betTextField.setEnabled(false);
	}


	public void setBetButtonEnabled() {
		this.betButton.setEnabled(true);
	}
	
	public void setBetButtonDisabled() {
		this.betButton.setEnabled(false);
	}

	public void setRaiseTextFieldEnabled() {
		this.raiseTextField.setEnabled(true);
	}

	public void setRaiseTextFieldDisabled() {
		this.raiseTextField.setEnabled(false);
	}

	public void setRaiseButtonEnabled() {
		this.raiseButton.setEnabled(true);
	}

	public void setRaiseButtonDisabled() {
		this.raiseButton.setEnabled(false);
	}

	public void setCallButtonEnabled() {
		this.callButton.setEnabled(true);
	}

	public void setCallButtonDisabled() {
		this.callButton.setEnabled(false);
	}

	public void setFoldButtonEnabled() {
		this.foldButton.setEnabled(true);
	}

	public void setFoldButtonDisabled() {
		this.foldButton.setEnabled(false);
	}

	public void setAllInButtonEnabled() {
		this.allInButton.setEnabled(true);
	}

	public void setAllInButtonDisabled() {
		this.allInButton.setEnabled(false);
	}
	
}
